package com.ebank.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ebank.model.MD5Encrypt;

public class EditPasswordCL extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		PrintWriter out = response.getWriter();
		String passwd=request.getParameter("passwd");
		if(session.getAttribute("username")!=null){
			Statement stat = null;
			Connection con=null;
			ResultSet rs=null;
			PreparedStatement pst = null;	
			String selectSQL = "select * from account";
			String loginName=(String) (session.getAttribute("username"));
			String pass=request.getParameter("pass");
			String MD5pw = MD5Encrypt.MD5_Ent.MD5(pass);
			System.out.print(MD5pw);
	try {
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection(
		      "jdbc:mysql://localhost/user?useUnicode=true&characterEncoding=Big5",
		      "root","root");
		 pst=con.prepareStatement("UPDATE account SET passwd=? where username=?");
		 pst.setObject(1, MD5pw);
		 pst.setObject(2, loginName);
		 pst.execute();
		 out.print("Success.......Please Wait");
		 response.setHeader("refresh", "2;URL=Main");
	} catch (Exception e) {
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				rs=null;
			}
			if(pst!=null){
				try {
					pst.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				pst=null;
			}
			if(con!=null){
				try {
					con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				con=null;
			}
		}}else{ out.print("Wong Please Login <a href=/EBanking/Login>Login</a>");
		} 
		}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
	}

}
